package threading;

public class Demo6 {

    public static final long COUNT = 1_000_000_000l;




    public static void main(String[] args) {
        //serial();
        concurrency();
    }
    //串行执行任务
    public static void serial() {

        long beg = System.currentTimeMillis();

        long a = 0;
        for (long i = 0;i < COUNT;i++) {
            a++;
        }

        a = 0;
        for (long i = 0;i < COUNT;i++) {
            a++;
        }

        long end = System.currentTimeMillis();
        System.out.println("执行时间间隔:" + (end - beg) + "ms" );


    }
    //并发

    public static void concurrency() {

        long beg = System.currentTimeMillis();

        Thread t1 = new Thread(() ->{
            long a = 0;
            for (long i = 0;i < COUNT;i++) {
                a++;
            }

        });
        Thread t2 = new Thread(() ->{
            long a = 0;
            for (long i = 0;i < COUNT;i++) {
                a++;
            }

        });
        t1.start();
        t2.start();

        //join阻塞等待线程的结束
        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();
        System.out.println("执行时间间隔:" + (end - beg) + "ms" );

    }
}
